package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.patchmanage.back.entity.NetConfiguration;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.INetConfigurationService;
import com.patchmanage.back.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

/**
 *
 * @author huizhao
 * @since 2021-04-14
 */
@Api(value = " 网络配置 controller", tags = {"通用配置 Api"})
@RestController
@RequestMapping("/back/netconfiguration")
public class NetConfigurationController {

    @Autowired
    public INetConfigurationService netConfigurationService;

    @Autowired
    public WebSocketServer webSocketServer;


    private static final Log log = LogFactory.get();

    /**
     * 获取网络配置接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取网络配置更新接口", notes = "获取网络配置更新接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<NetConfiguration> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(netConfigurationService.getById(dict.getInt("id")));
    }


    /**
     * 新增网络配置更新接口
     * @param netConfiguration
     * @return
     */
    @ApiOperation(value = "新增网络配置更新接口", notes = "新增网络配置更新接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public Result<Boolean> saveOrUpdate(@RequestBody NetConfiguration netConfiguration){
        netConfiguration.setUpdateTime(LocalDateTime.now());
        return Result.success(netConfigurationService.saveOrUpdate(netConfiguration));
    }

    /**
     * 获取系统网卡配置列表
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取系统网卡配置列表", notes = "获取系统网卡配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hostName", value = "hostName", required = true, dataType = "String", paramType = "body")
    })
    @RequestMapping(value = "getNetWorkCardList", method = RequestMethod.POST)
    public Result<List<JSONObject>> getNetWorkCardList(@ApiIgnore @RequestBody Dict dict){
        String hostName = dict.getStr("hostName");

        /**获取网卡列表**/
        List<JSONObject> netCardList = new ArrayList<JSONObject>();



        /** end **/

        return Result.success(netCardList);
    }

    /**
     * 网络探测
     * @param dict
     * @return
     */
    @ApiOperation(value = "网络探测", notes = "网络探测")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "probeType(0.ping,1.telnet,2.ssh,3.traceroute)", value = "probeType", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "probeUrl", value = "probeUrl", required = true, dataType = "String", paramType = "body"),

    })
    @RequestMapping(value = "networkDetection", method = RequestMethod.POST)
    public Result<List<String>> networkDetection(@ApiIgnore @RequestBody Dict dict) throws Exception{

        Integer probeType = dict.getInt("probeType");
        String probeUrl = dict.getStr("probeUrl");
        List<String> returnContent = null;
        if(probeType == 0){
            //ping
            String osName = System.getProperty("os.name");//获取操作系统类型
            log.info("====================网络探测操作系统："+osName+"========================");
            if(osName.toLowerCase().contains("linux")){
                String cmd = "ping -c 5 " + probeUrl;
                log.info("====================网络探测执行命令："+cmd+"========================");
                returnContent = RuntimeUtil.execForLines(cmd);
            }

        }else if(probeType == 1){
            //telnet
            String osName = System.getProperty("os.name");//获取操作系统类型
            log.info("====================网络探测操作系统："+osName+"========================");
            if(osName.toLowerCase().contains("linux")){
                String cmd = "telnet " + probeUrl;
                log.info("====================网络探测执行命令："+cmd+"========================");
                returnContent = RuntimeUtil.execForLines(cmd);
            }

        }else if(probeType == 2){
            //ssh

        }else if(probeType == 3){
            //traceroute
            String osName = System.getProperty("os.name");//获取操作系统类型
            log.info("====================网络探测操作系统："+osName+"========================");
            if(osName.toLowerCase().contains("linux")){
                String cmd = "traceroute " + probeUrl;
                log.info("====================网络探测执行命令："+cmd+"========================");
                returnContent = RuntimeUtil.execForLines(cmd);
            }

        }


        for(String s : returnContent){
            webSocketServer.sendMessage(s);
        }
        return Result.success(returnContent);
    }
}
